package com.bluesea.report.controller;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.bluesea.report.entity.User;
import com.bluesea.report.service.user.impl.UserserviceImpl;

@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private UserserviceImpl userservice;
	
	@Autowired
	private HttpServletResponse res;
	
	@RequestMapping("/showUser")
	public String showUser(User u, Model model) {
		System.out.println("hello worlld!");
		int userId = u.getId();
		User user = this.userservice.getUserByid(userId);
		model.addAttribute("user",user);
		return user.getUserName();
		
	}
	
	/**
	 * 登录测试
	 * @param user
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/dologin", method=RequestMethod.POST)
	public String dologin(User user, Model model) {
		System.out.println("----controller登录参数：----"+user);
		if(userservice.doUserLogin(user)) {
			model.addAttribute("successMsg", "登录成功！");//返回到页面所夹带的参数
			model.addAttribute("name", user.getUserName());
			return "/success";//返回的页面
		}else {
			model.addAttribute("failMsg", "用户不存在或密码错误！");
			return "/fail";
		}
		
	}
	
	@RequestMapping(value = "/login", method=RequestMethod.POST)
	@ResponseBody
	public String login(@RequestBody User user) {
		String flg = null;
		System.out.println("----controller登录参数：----"+user);
		if(userservice.doUserLogin(user)) {
			System.out.println(">>>>>"+userservice.doUserLogin(user));
			flg = "success";
			return JSONArray.toJSONString(flg);
		}else {
			flg = "false";
			return JSONArray.toJSONString(flg);
		}
	}
}
